<?php
declare (strict_types = 1);
namespace app\adminapi\logic\setting;
use adminmatrix\server\app\logic\BaseLogic;
use adminmatrix\annotation\Inject;
use app\adminapi\model\setting\AdminSetting;
use app\adminapi\model\setting\AdminSettingConfig;
use think\Request;

class WebLogic extends BaseLogic
{
    #[Inject]
    protected AdminSetting $model;

    public function get(Request $request) : array | \think\model\Collection
    {
        $where =[];
        if ($request->get('tag' , ''))
        {
            $where[] = ['tag' ,'=' , $this->request->get('tag')];
        }
        return $this->model ->with('setting') -> where($where)->select()->toArray();
    }


    public function set(Request $request) : bool
    {
        $data = [];
        foreach ($request->put() as $name => $value)
        {
            $data[] = (new AdminSettingConfig)->where('name' , $name)->save(['value'=>$value]);
        }

        return true;
    }
}